package com.example.sakurasay.settings

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

class SettingsDataStore(context: Context) {

    private val dataStore = context.dataStore

    companion object {
        val PASSWORD_BOOK_MODE_KEY = intPreferencesKey("password_book_mode")
    }

    val passwordBookMode: Flow<Int> = dataStore.data
        .map { preferences ->
            preferences[PASSWORD_BOOK_MODE_KEY] ?: 0
        }

    suspend fun savePasswordBookMode(modeIndex: Int) {
        dataStore.edit { settings ->
            settings[PASSWORD_BOOK_MODE_KEY] = modeIndex
        }
    }
}